<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :class="{active: isActive}"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    path: String
  },
  data() {
    return {
      // isActive: false
    };
  },
  mounted() {},
  methods: {
    itemClick() {
      //抛出错误
      if(this.$route.path !=this.path){
        this.$router.replace(this.path)
      console.log(this.$route.path)
      console.log(this.path)
      }
    }
  },
  computed: {
    isActive() {
      // home -> item1(/home) true
      // home -> item1(/cart) false
      // home -> item1(/mine) false
      return this.$route.path.indexOf(this.path) != -1;
    }
  } 
};
</script>

<style scoped>

.tab-bar-item {
  flex: 1;
  text-align: center;
  /* 行高 */
  /* line-height: 49px; */
  font-size: 14px;
}
.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  /* 去除图片下方多余的空间 */
  vertical-align: middle;
}
.active {
  color: red;
}
</style>